package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.CaAppUserInfoAddBo;
import com.ruoyi.system.bo.CaAppUserInfoQueryBo;
import com.ruoyi.system.bo.CaAppUserInfoEditBo;
import com.ruoyi.system.domain.CaAppUserInfo;
import com.ruoyi.system.mapper.CaAppUserInfoMapper;
import com.ruoyi.system.vo.CaAppUserInfoVo;
import com.ruoyi.system.service.ICaAppUserInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 小程序用户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Service
public class CaAppUserInfoServiceImpl extends ServicePlusImpl<CaAppUserInfoMapper, CaAppUserInfo> implements ICaAppUserInfoService {

    @Override
    public CaAppUserInfoVo queryById(Long userId){
        return getVoById(userId, CaAppUserInfoVo.class);
    }

    @Override
    public TableDataInfo<CaAppUserInfoVo> queryPageList(CaAppUserInfoQueryBo bo) {
        PagePlus<CaAppUserInfo, CaAppUserInfoVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), CaAppUserInfoVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<CaAppUserInfoVo> queryList(CaAppUserInfoQueryBo bo) {
        return listVo(buildQueryWrapper(bo), CaAppUserInfoVo.class);
    }

    private LambdaQueryWrapper<CaAppUserInfo> buildQueryWrapper(CaAppUserInfoQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaAppUserInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), CaAppUserInfo::getUserName, bo.getUserName());
        lqw.eq(bo.getRoleId() != null, CaAppUserInfo::getRoleId, bo.getRoleId());
        lqw.eq(StrUtil.isNotBlank(bo.getAccount()), CaAppUserInfo::getAccount, bo.getAccount());
        lqw.eq(StrUtil.isNotBlank(bo.getPassword()), CaAppUserInfo::getPassword, bo.getPassword());
        lqw.eq(bo.getSex() != null, CaAppUserInfo::getSex, bo.getSex());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), CaAppUserInfo::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getEmail()), CaAppUserInfo::getEmail, bo.getEmail());
        lqw.eq(bo.getBirthday() != null, CaAppUserInfo::getBirthday, bo.getBirthday());
        lqw.eq(bo.getScore() != null, CaAppUserInfo::getScore, bo.getScore());
        lqw.eq(StrUtil.isNotBlank(bo.getPicture()), CaAppUserInfo::getPicture, bo.getPicture());
        lqw.eq(bo.getIsmanager() != null, CaAppUserInfo::getIsmanager, bo.getIsmanager());
        lqw.eq(bo.getLastTime() != null, CaAppUserInfo::getLastTime, bo.getLastTime());
        lqw.eq(StrUtil.isNotBlank(bo.getLastIp()), CaAppUserInfo::getLastIp, bo.getLastIp());
        lqw.eq(StrUtil.isNotBlank(bo.getLastLocation()), CaAppUserInfo::getLastLocation, bo.getLastLocation());
        lqw.eq(bo.getLocked() != null, CaAppUserInfo::getLocked, bo.getLocked());
        lqw.eq(bo.getLockerId() != null, CaAppUserInfo::getLockerId, bo.getLockerId());
        lqw.eq(bo.getLockTime() != null, CaAppUserInfo::getLockTime, bo.getLockTime());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(CaAppUserInfoAddBo bo) {
        CaAppUserInfo add = BeanUtil.toBean(bo, CaAppUserInfo.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(CaAppUserInfoEditBo bo) {
        CaAppUserInfo update = BeanUtil.toBean(bo, CaAppUserInfo.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CaAppUserInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
